﻿var CardModel = function() {
    this.Name = "";
    this.IsAvailable = false;
};
var PowerModel = function (name, count) {
    this.Name = name;
    this.Count = count;
};
var WariorModel = function (warior) {
    this.Name = "empty";
    this.Health = 0;
    if (warior != null) {
        this.Name = warior.Name;
        this.Health = warior.Health;
    }
};
var ArenaModel = Backbone.Model.extend({
    defaults: {
        RivalWariors: [],
        OwnerWariors: [],
        Cards: [],
        OwnerPower: [],
        RivalPower: [],
        Animation: []
    },
    url: "/Home/Arena?gameId=0",
    initialize: function () {
    },
    parse: function (response) {
        var attrs = {};
        attrs.OwnerPower = [];
        attrs.OwnerPower.push(new PowerModel("Fire", response.OwnerPower.Fire));
        attrs.OwnerPower.push(new PowerModel("Water", response.OwnerPower.Water));
        attrs.OwnerPower.push(new PowerModel("Air", response.OwnerPower.Air));
        attrs.OwnerPower.push(new PowerModel("Earth", response.OwnerPower.Earth));
        attrs.OwnerPower.push(new PowerModel("Special", response.OwnerPower.Special));
        attrs.RivalPower = [];
        attrs.RivalPower.push(new PowerModel("Fire", response.RivalPower.Fire));
        attrs.RivalPower.push(new PowerModel("Water", response.RivalPower.Water));
        attrs.RivalPower.push(new PowerModel("Air", response.RivalPower.Air));
        attrs.RivalPower.push(new PowerModel("Earth", response.RivalPower.Earth));
        attrs.RivalPower.push(new PowerModel("Special", response.RivalPower.Special));
        attrs.OwnerWariors = [];
        for (i = 0; i < response.OwnerWariors.length; i++) {
            attrs.OwnerWariors.push(new WariorModel(response.OwnerWariors[i]));
        }
        attrs.RivalWariors = [];
        for (i = 0; i < response.RivalWariors.length; i++) {
            attrs.RivalWariors.push(new WariorModel(response.RivalWariors[i]));
        }


        return attrs;
    }
});

var ArenaView = Backbone.View.extend({
    el: $('#content'),
    TemplateText: '',
    initialize: function () {
        $(this.el).off();
        this.undelegateEvents();
        this.model.bind("change", this.render);
        this.model.view = this;
        this.model.fetch();
    },
    render: function () {
        var model = this;
        if (model.view.TemplateText.toString().length == 0) {
            $.post("/Home/ArenaTemplate", function (template) {
                model.view.TemplateText = template;
                var compiledTemplate = _.template(model.view.TemplateText);
                $(model.view.el).html(compiledTemplate(model.toJSON()));
            });
        } else {
            var compiledTemplate = _.template(model.view.TemplateText);
            $(model.view.el).html(compiledTemplate(model.toJSON()));
        }
    },
    events: {
        "click form button": "save"
    },
    save: function (event) {
    }
});
